//查询学员信息
function renderStudent() {
    $('tbody').html('');
    axios({
        method: 'GET',
        url: '/student/list'
    }).then(function({ data: res }) {
        let data = res.data
            // console.log(data)
        data.forEach(item => {
            let newTr = $(`<tr>
            <th scope="row">${item.id}</th>
            <td>${item.name}</td>
            <td>${item.age}</td>
            <td>${item.sex}</td>
            <td>${item.group}</td>
            <td>${item.phone}</td>
            <td>${item.salary}</td>
            <td>${item.truesalary}</td>
            <td>${item.province}${item.city}${item.county}</td>
            <td>
              <button type="button" class="btn btn-primary btn-sm edit"  data-id="${item.id}">修改</button>
              <button type="button" class="btn btn-danger btn-sm" data-id="${item.id}">删除</button>
            </td>
          </tr>`);
            $('tbody').append(newTr)
        });
    })
}
renderStudent()



//----删除学员------------------------------------------------
function deleteStudent() {
    $('tbody').on('click', '.btn-danger', function() {
        // console.log($(this).data('id'))
        axios({
            method: 'delete',
            url: '/student/delete',
            params: {
                id: $(this).data('id'),
            }

        }).then(function({ data: res }) {
            // console.log(res)
            if (res.code === 0) {
                $('tbody').text('')
                renderStudent()
                toastr.success(res.message)
            }
        })
    })
}
deleteStudent()

//--录入新学员---------------------------------------------------------
//录入新学员中--省市县联动小功能
axios({
    method: 'GET',
    url: '/geo/province'
}).then(function({ data: res }) {
    let newoption = ''
    $.each(res, function(index, item) {
        newoption += `<option  value="${item}">${item}</option>`
    });
    $('[name="province"]').append(newoption);
});

$('[name="province"]').on('change', function() {
    $('[name="city"]').html('<option selected value="">--市--</option>');
    $('[name="county"]').html('<option selected value="">--县--</option>');
    if ($(this).val() == '') {
        return
    }
    axios({
        method: 'GET',
        url: '/geo/city',
        params: { pname: $(this).val() }
    }).then(function({ data: res }) {
        let newoption = ''
        $.each(res, function(index, item) {
            newoption += `<option  value="${item}">${item}</option>`
        });
        $('[name="city"]').append(newoption)
    });
});



$('[name="city"]').on('change', function() {
    $('[name="county"]').html('<option selected value="">--县--</option>');
    axios({
        method: 'GET',
        url: '/geo/county',
        params: {
            pname: $(this).parent().prev().children().val(),
            cname: $(this).val()
        }
    }).then(function({ data: res }) {
        // $('[name="county"]').html('<option selected value="">--县--</option>');
        let newoption = ''
        $.each(res, function(index, item) {
            newoption += `<option  value="${item}">${item}</option>`
        });
        $('[name="county"]').append(newoption)
    });
});
//省市县联动功能完-----

//添加学员信息功能
$('.add-form .btn-primary').on('click', function() {
    // console.log($('.add-form').serialize())
    axios({
        method: 'POST',
        url: '/student/add',
        data: $('.add-form').serialize()
    }).then(function({ data: res }) {
        // console.log(res);
        if (res.code === 0) {
            toastr.success(res.message);
        } else if (res.code === 1) {
            toastr.error(res.message);
        }
    })
    renderStudent();
    return false;
})



//修改学员信息
$('tbody').on('click', '.edit', function() {
    $('#updateModal').modal('show');
    let id = $(this).data('id');
    // console.log(id);
    //信息回填
    axios({
        method: 'get',
        url: '/student/one',
        params: { id }
    }).then(function({ data: res }) {
        // console.log(res);
        $('#updateModal input[name="id"]').val(res.data.id);
        $('#updateModal input[name="name"]').val(res.data.name);
        $('#updateModal input[name="age"]').val(res.data.age);

        $('#updateModal input[name="phone"]').val(res.data.phone);
        $('#updateModal input[name="salary"]').val(res.data.salary);
        $('#updateModal input[name="truesalary"]').val(res.data.truesalary);



        $('#updateModal select[name="province"]').val(res.data.province);
        $('#updateModal select[name="city"]').append(`<option selected value="${res.data.city}">${res.data.city}</option>`);
        $('#updateModal select[name="county"]').append(`<option selected value="${res.data.county}">${res.data.county}</option>`);
        // $('#updateModal select[name="county"]').val(res.data.county);


        $('#updateModal select[name=group]').val(res.data.group);

        $(`#updateModal input[name=sex][value=${res.data.sex}]`).prop('checked', true)
    });

    $('.update-form').bootstrapValidator(student()).on('success.form.bv', function(e) {
        e.preventDefault();
        //提交信息
        let data = $(this).serialize();
        axios.put('/student/update', data).then(({ data: res }) => {
            // console.log(res);
            if (res.code === 0) {
                toastr.success(res.message);
                renderStudent();
                $('#updateModal').modal('hide')
            }
        })
    });

})

function student() {
    return {
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '姓名不能为空',
                    },
                    stringLength: {
                        min: 2,
                        max: 10,
                        message: '姓名长度2~10位'
                    }
                }
            },
            age: {
                validators: {
                    notEmpty: {
                        message: '年龄不能为空',
                    },
                    greaterThan: {
                        value: 18,
                        message: '年龄不能小于18岁'
                    },
                    lessThan: {
                        value: 60,
                        message: '年龄不能超过60岁'
                    }
                }
            },
            sex: {
                validators: {
                    choice: {
                        min: 1,
                        max: 1,
                        message: '请选择性别'
                    }
                }
            },
            group: {
                validators: {
                    notEmpty: {
                        message: '组号不能为空',
                    },
                    regexp: {
                        regexp: /^\d{1,2}$/,
                        message: '请选择有效的组号'
                    }
                }
            },
            phone: {
                validators: {
                    notEmpty: {
                        message: '手机号不能为空',
                    },
                    regexp: {
                        regexp: /^1\d{10}$/,
                        message: '请填写有效的手机号'
                    }
                }
            },
            salary: {
                validators: {
                    notEmpty: {
                        message: '实际薪资不能为空',
                    },
                    greaterThan: {
                        value: 800,
                        message: '期望薪资不能低于800'
                    },
                    lessThan: {
                        value: 100000,
                        message: '期望薪资不能高于100000'
                    }
                }
            },
            truesalary: {
                validators: {
                    notEmpty: {
                        message: '实际薪资不能为空',
                    },
                    greaterThan: {
                        value: 800,
                        message: '实际薪资不能低于800'
                    },
                    lessThan: {
                        value: 100000,
                        message: '实际薪资不能高于100000'
                    }
                }
            },
            province: {
                validators: {
                    notEmpty: {
                        message: '省份必填',
                    },
                }
            },
            city: {
                validators: {
                    notEmpty: {
                        message: '市必填',
                    },
                }
            },
            county: {
                validators: {
                    notEmpty: {
                        message: '县必填',
                    },
                }
            },
        }
    }
}